﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
    /// <summary>
    /// BasePage 的摘要说明
    /// </summary>
    public class BasePage : System.Web.UI.Page
    {
        public BasePage():base()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        public string BaseUrl
        {
            get
            {
                string sUrl = Request.Url.AbsoluteUri;
                return sUrl.Substring(0, sUrl.LastIndexOf("/"));
            }
        }

        /// <summary>
        /// 身份证号
        /// </summary>
        public string UserName
        {
            get
            {
                if (Session["userName"] != null)
                    return Session["userName"].ToString();
                else
                    return "";
            }
            set
            {
                Session["userName"] = value;
            }
        }
        public string IDCard
        {
            get
            {
                if (Session["studIdcard"] != null)
                {
                    return Session["studIdcard"].ToString();
                }
                else
                {
                    return "";
                }
            }
            set
            {
                Session["studIdcard"] = value;
            }
        }
        ///// <summary>
        ///// 登录人ID，对应Users里的USERID
        ///// </summary>
        //public string USERID
        //{
        //    get
        //    {
        //        if (Session["USERID"] != null)
        //            return Session["USERID"].ToString();
        //        else
        //            return "";
        //    }
        //    set
        //    {
        //        Session["USERID"] = value;
        //    }
        //}


        ///// <summary>
        ///// 权限类型（可能还有点问题）
        ///// </summary>
        //public enum PermissionType { None=0,View = 1,Insert =3,Edit =7,Delete = 15,Owner = 16 }

        //public PermissionType Permission
        //{
        //    get
        //    {
        //        if (EditFlag)
        //        {
        //            //根据角色权限表，根据用户数据去查询数据库，目前返回所有者
        //            return PermissionType.Owner;
        //        }
        //        else
        //            return PermissionType.View;
        //    }
        //}

        #region 页面加载流程
        protected override void OnLoad(EventArgs e)
        {
            BaseLoad();
            base.OnLoad(e);
        }
        private void BaseLoad()
        {
            Page_LoadAlwayInit();

            Page_LoadAlways();

            if (IsPostBack)
            {
                Page_LoadPostBack();
            }
            else
            {
                Page_LoadOnce();
            }
            Page_LoadAlwaysEnd();
        }

        private void Page_Load(object sender, System.EventArgs e)
        {
            Page_LoadAlwayInit();

            Page_LoadAlways();

            if (IsPostBack)
            {
                Page_LoadPostBack();
            }
            else
            {
                Page_LoadOnce();
            }
        }

        protected virtual void Page_LoadOnce()
        {
            
        }

        protected virtual void Page_LoadPostBack()
        {

        }

        protected virtual void Page_LoadAlways()
        {
            if (UserName == string.Empty)
            {
                Response.Redirect(BSUtility.mainUrl + "Users/UserLogin.aspx");
            }
        }

        private void Page_LoadAlwayInit()
        {
            //if (!EditFlag)
            //{
            //    AlertMessage("提示","当前不在可编辑范围时间内，若需要修改，请修改时点属性，若仅需浏览，请忽略该信息。");
            //}
        }
        protected virtual void Page_LoadAlwaysEnd()
        {

        }

        #endregion

        #region 基类方法

        public void AlertMessage(string title, string msg)
        {
            msg = msg.Replace("\\", "\\\\");
            msg = msg.Replace("\r\n", "\\n");
            msg = msg.Replace("\n", "\\n");
            msg = msg.Replace("\"", "\\\"");
            string script = "";
            if (title == null)
                script = "<script>Ext.onReady(function(){Ext.MessageBox.alert('" + msg + "');})</script>";
            else
                script = "<script>Ext.onReady(function(){Ext.MessageBox.alert('" + title + "', '" + msg + "');})</script>";
            ClientScript.RegisterStartupScript(this.GetType(), System.Guid.NewGuid().ToString(), script);
        }

        /// <summary>
        /// 弹出页面
        /// </summary>
        /// <param name="url"></param>
        public void OpenUrl(string url)
        {
            string script = string.Format("<script language='JavaScript'>window.open('" + "{0}" + "','" + "','');<", url);
            script += "/";
            script += "script>";

            //ScriptManager.RegisterStartupScript(this, this.GetType(), System.Guid.NewGuid().ToString(), script, false);
        }

        /// <summary>
        /// 返回上一页
        /// </summary>
        public void JavascriptGoBack()
        {
            string script = "<script>history.go(-1);</script>";
            ClientScript.RegisterStartupScript(this.GetType(), System.Guid.NewGuid().ToString(), script);
        }

        /// <summary>
        /// 执行JavaScript方法
        /// </summary>
        /// <param name="script"></param>
        public void JavascriptFun(string script)
        {
            string AllScript = string.Format("<script>{0}</script>", script);
            ClientScript.RegisterStartupScript(this.GetType(), System.Guid.NewGuid().ToString(), AllScript);
        }

        /// <summary>
        /// 关闭窗口
        /// </summary>
        public void JavascriptClose()
        {
            string script = "<script>window.opener = null;window.close();</script>";
            ClientScript.RegisterStartupScript(this.GetType(), System.Guid.NewGuid().ToString(), script);
        }

        /// <summary>
        /// 跳转到其他页面
        /// </summary>
        /// <param name="url">页面地址</param>
        public void JavascriptGoUrl(string url)
        {
            string script = string.Format("<script>location = '{0}';</script>", url);
            ClientScript.RegisterStartupScript(this.GetType(), System.Guid.NewGuid().ToString(), script);
        }

        /// <summary>
        /// 显示信息并跳转页面
        /// </summary>
        /// <param name="message"></param>
        /// <param name="url"></param>
        public void ShowMessageAndGoUrl(string message, string url)
        {
            string script = string.Format("<script>alert('{0}');location.href='{1}';</script>", message, url);
            ClientScript.RegisterStartupScript(this.GetType(), System.Guid.NewGuid().ToString(), script);
        }

        /// <summary>
        /// 获取MD5值
        /// </summary>
        /// <param name="data">原字符串</param>
        /// <returns>通过MD5加密后的</returns>
        public string GetMd5Code(string data)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(data, "md5").ToUpper();
        }

        /// <summary>
        /// 返回上一页
        /// </summary>
        protected bool GoBack()
        {
            if (ViewState["BackUrl"] != null)
            {
                Response.Redirect(ViewState["BackUrl"].ToString());
                return true;
            }
            else
                return false;
        }

        ///// <summary>
        ///// 将列转化成字符串形式用逗号分隔
        ///// </summary>
        ///// <param name="columns">列字符串</param>
        //public string GetTableColumns(DataColumnCollection columns)
        //{
        //    string title = "";
        //    foreach (DataColumn col in columns)
        //    {
        //        title += GetSafeString(col.ColumnName).Replace(',', '_') + ",";
        //    }
        //    return title.Remove(title.Length - 1);
        //}

        public static string CutString(string str, int maxlength)
        {
            if (str.Length > maxlength)
                return str.Substring(0, maxlength) + "....";
            else return str;
        }

        

        #endregion 基类方法

    }
